package com.gameShare.utils.ffmpeg

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import java.io.BufferedReader
import java.io.InputStreamReader

@Component
class CmdExecutor() {

    val logger: Logger = LoggerFactory.getLogger(CmdExecutor::class.java)


    fun executeCmd(cmd: String): Pair<String, String> {
        val processBuilder = ProcessBuilder(cmd.split(" "))
        val process = processBuilder.start()

        // 读取标准输出
        val outputReader = BufferedReader(InputStreamReader(process.inputStream))
        val output = outputReader.use { it.readText() }

        // 读取错误输出
        val errorReader = BufferedReader(InputStreamReader(process.errorStream))
        val errorOutput = errorReader.use { it.readText() }
        logger.debug("exec $cmd")

        // 等待进程结束
        process.waitFor()

        return Pair(output, errorOutput)
    }

}